package com.enzoism.spring.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @program: Java_Spring01
 * @description: 自定义数据库源
 * @author: zhifeng
 * @create: 2020-12-23 19:17
 */
@Configuration
public class DataSourceConfig {

    @Bean(name = "druidDataSource")
    public DataSource createDruidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/spring-user?useSSL=false&useUnicode=true&characterEncoding=utf8");
        return druidDataSource;
    }

    @Bean(name = "druidJdbcTemplate")
    public JdbcTemplate druidJdbcTemplate(@Qualifier("druidDataSource") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
}